//app.js
// var apiHost = 'http://develop.jhjvip.cn:8080/mall/'; //外部接口
var apiHost = 'https://www.uhuijia.com.cn/mall/'
 //var apiHost = 'http://192.168.1.107:8080/mall/'
//  var apiHost = 'http://192.168.1.193:8080/mall/'

var apiUpLoad = 'https://www.uhuijia.com.cn/'

var Appid = 'wx910d63f3cec2d59b'; //appid
var Secret = '3f8216c7ada3554a4701e67e729b839b'; //Secret
var yunId = '1' ;     //云id

App({
  onLaunch: function () {

    var that = this;
    // 展示本地存储能力
    var logs = wx.getStorageSync('logs') || []
    logs.unshift(Date.now())

    wx.setStorageSync('apiHost', apiHost);
    wx.setStorageSync('apiUpLoad', apiUpLoad);
    wx.setStorageSync('logs', logs);
    wx.setStorageSync('yunId', yunId);

    wx.setStorageSync('Appid', Appid);
    wx.setStorageSync('Secret', Secret);


    //1、调用微信登录接口，获取code
    wx.login({
        success: function (r) {
          var code = r.code;//登录凭证

          if (code) {
            that.getUseInfo(code)   //获取用户信息
          } else {
            console.log('获取用户登录态失败！' + r.errMsg)
          }
        },
        fail: function () {
            console.log('登陆失败')
        }
    })
  },
  /**
   * 获取用户信息
   */
  getUseInfo : function(code){
    var that = this;

    //2、调用获取用户信息接口
    wx.getUserInfo({
      success: function (res) {
        that.VERBurl(res, code)   //解密用户登录完成后的url
      },
      fail: function (res) {
        // wx.navigateBack({          //如果没有授权直接关闭小程序
        //   delta: -1
        // })
        console.log('获取用户信息失败')
      }
    })
  },
  /**
   * 解密用户登录完成后的url
   */
  VERBurl: function (res, code){
      var that = this;
      wx.showLoading({
        title: '加载中',
        icon: 'loading',
      });
    //3.请求自己的服务器，解密用户信息 获取unionId等加密信息  (注意：这个东西必须需要 开放平台 授权)
    wx.request({
      url: apiHost + 'xcx/UserInfo/decodeUserInfo',//自己的服务接口地址
      method: 'post',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: { 
        encryptedData: res.encryptedData, 
        iv: res.iv, 
        code: code,
        Appid: Appid,
        Secret: Secret
      },
      success: function (data) {
        //4.解密成功后 获取自己服务器返回的结果
        if (data.data.status == 1) {
          var userInfo_ = data.data.userInfo;
          var openIdFl = userInfo_.openId;
          var phone = userInfo_.phone;

          that.globalData.openId = openIdFl;
          if (that.employIdCallback) {
            that.employIdCallback(userInfo_.openId);
          }
          
          wx.setStorageSync('userId', userInfo_.unionId);
          wx.setStorageSync('phone', phone);
          wx.setStorageSync('openId', userInfo_.openId);
          wx.setStorageSync('session_token', userInfo_.session_key);
          wx.setStorageSync('userInfo', userInfo_);

          that.platformLogin(userInfo_)  //平台登录
        } else {
          console.log('解密失败')
        }
        wx.hideLoading();   //隐藏加载loading
      },
      fail: function () {
        wx.hideLoading();   //隐藏加载loading
        wx.showToast({
          title: '网络异常',
          icon: 'none',
          duration: 5000,
          mask: true
        });
      }
    })
  },
  /**
   * 平台登录
   */
  platformLogin: function (userInfo_){
    var openId = userInfo_.openId

    console.log(openId +"----------------openId")
    
    wx.showLoading({
      title: '加载中',
      icon: 'loading',
    });
    wx.request({
      url: apiHost + 'xcx/login/in',//自己的服务接口地址
      method: 'post',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: { 
        openId: openId, 
        yunId: yunId
      },
      success: function (data) {
        var code = data.data.code;     //调用是否成功标识
        var dataFlag = data.data.data; //判断是云店主的标识
        wx.hideLoading();   //隐藏加载loading

        //如果调用成功 判断跳转
        if (code == "10000"){
          // 跳转云店主主页
          if (dataFlag == "storeBoss"){
            wx.navigateTo({
              url: '../yunShopIndex/yunShopIndex',
              success: function () {
                wx.hideLoading();   //隐藏加载loading
              },  
            });
          } else
          // 跳转普通用户主页
          if (dataFlag == "userCommon"){
            wx.switchTab({
              url: '../Zindex/Zindex',
              success: function () {
                wx.hideLoading();   //隐藏加载loading
              },  
            });
          }
        }else{
          wx.navigateTo({
            url: '../login/login?openId=' + openId,
            success: function () {
              wx.hideLoading();   //隐藏加载loading
            },  
          });
        }
      },
      fail: function () {
        wx.hideLoading();   //隐藏加载loading
        wx.showToast({
          title: '网络异常',
          icon: 'none',
          duration: 5000,
          mask: true
        });
      }
    })
  },
  globalData: {
    openId: '',
    onemoreFlag:false
  }
})